/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastrarReceita.java
 *
 * Created on 10/05/2009, 21:24:31
 */

package gerenciadorfinanceiro.view.resources;

import gerenciadorfinanceiro.Utilitario;
import gerenciadorfinanceiro.database.resources.Income;
import java.awt.event.KeyEvent;
import java.util.Date;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author eduardo
 */
public class CadastrarReceita extends javax.swing.JDialog {

    /** Creates new form CadastrarReceita */
    public CadastrarReceita() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblDescricao = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescricao = new javax.swing.JTextArea();
        lblData = new javax.swing.JLabel();
        txtData = new javax.swing.JFormattedTextField();
        lblValor = new javax.swing.JLabel();
        txtValor = new javax.swing.JFormattedTextField();
        btnCadastrar = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gerenciadorfinanceiro.GerenciadorFinanceiroApp.class).getContext().getResourceMap(CadastrarReceita.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        lblDescricao.setText(resourceMap.getString("lblDescricao.text")); // NOI18N
        lblDescricao.setName("lblDescricao"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtDescricao.setColumns(20);
        txtDescricao.setRows(5);
        txtDescricao.setName("txtDescricao"); // NOI18N
        txtDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDescricaoKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(txtDescricao);

        lblData.setText(resourceMap.getString("lblData.text")); // NOI18N
        lblData.setName("lblData"); // NOI18N

        txtData.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM))));
        txtData.setName("txtData"); // NOI18N

        lblValor.setText(resourceMap.getString("lblValor.text")); // NOI18N
        lblValor.setName("lblValor"); // NOI18N

        txtValor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        txtValor.setName("txtValor"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gerenciadorfinanceiro.GerenciadorFinanceiroApp.class).getContext().getActionMap(CadastrarReceita.class, this);
        btnCadastrar.setAction(actionMap.get("Salvar")); // NOI18N
        btnCadastrar.setText(resourceMap.getString("btnCadastrar.text")); // NOI18N
        btnCadastrar.setName("btnCadastrar"); // NOI18N

        btnSair.setAction(actionMap.get("Sair")); // NOI18N
        btnSair.setText(resourceMap.getString("btnSair.text")); // NOI18N
        btnSair.setName("btnSair"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDescricao)
                            .addComponent(lblData)
                            .addComponent(lblValor))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtData, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
                            .addComponent(txtValor, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescricao)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblData))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblValor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCadastrar)
                    .addComponent(btnSair))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        txtDescricao.setText("");
        txtData.setValue(new Date());
        txtValor.setText("");
        txtDescricao.requestFocus();
    }//GEN-LAST:event_formComponentShown

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        txtDescricao.setText("");
        txtData.setValue(new Date());
        txtValor.setText("");
        txtDescricao.requestFocus();
    }//GEN-LAST:event_formWindowOpened

    private void txtDescricaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescricaoKeyPressed
        int keyPressed = evt.getKeyCode();

        if (keyPressed == KeyEvent.VK_TAB) {
            txtData.requestFocus();
            evt.consume();
        }
    }//GEN-LAST:event_txtDescricaoKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnSair;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblData;
    private javax.swing.JLabel lblDescricao;
    private javax.swing.JLabel lblValor;
    private javax.swing.JFormattedTextField txtData;
    private javax.swing.JTextArea txtDescricao;
    private javax.swing.JFormattedTextField txtValor;
    // End of variables declaration//GEN-END:variables

    private Utilitario util;

    /**
     * @return the util
     */
    public Utilitario getUtil() {
        return util;
    }

    /**
     * @param util the util to set
     */
    public void setUtil(Utilitario util) {
        this.util = util;
    }

    @Action
    public void Sair() {
        this.setVisible(false);
    }

    @Action
    public void Salvar() {
        Income receita = new Income();
        receita.setDescriptionIncome(txtDescricao.getText());
        receita.setData((Date)txtData.getValue());

        Object o = txtValor.getValue();
        Double valor;
        if (o.getClass().equals(Double.class)) {
            valor = (Double)o;
        } else if (o.getClass().equals(Long.class)) {
            double aux = ((Long)o).longValue();
            valor = aux;
        } else {
            JOptionPane.showMessageDialog(this, "Ocorreu um erro ao converter o valor da receita.");
            return;
        }

        receita.setValue(valor);

        receita.setOwner(util.getLoggedUser());

        util.getIncomeDAO().saveIncome(receita);
        JOptionPane.showMessageDialog(this, "Receita gravada com sucesso.");
    }

}
